package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.entity.MainAccount;
import com.yuyou.fn.settlement.service.IMainAccountService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@EventHandler(tables = {"yyfn.teacher", "yyfn.student", "yyfn.area_team", "yyfn.bus_team"})
@Component
public class MainAccountUpdater implements IEventHandler {

    @Resource
    private IMainAccountService mainAccountService;

    @Override
    public void insert(String tableName, JSONArray rowList) {
        insertOrUpdate(tableName, rowList);
    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        insertOrUpdate(tableName, rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }

    public void insertOrUpdate(String tableName, JSONArray rowList) {
        for (int i = 0; i < rowList.size(); i++) {

            MainAccount mainAccount = new MainAccount();
            mainAccount.setCreateTime(TimeUtils.now());
            mainAccount.setUpdateTime(TimeUtils.now());
            JSONObject row = rowList.getJSONObject(i);
            if (tableName.contains("area_team")) {
                mainAccount.setMainAccountId(row.getLong("area_team_id"));
                mainAccount.setName(row.getString("name"));
                mainAccount.setParentMainAccountId(1L);//誉优公司
                mainAccount.setTag("areaTeam");
                mainAccount.setAreaTeamId(row.getLong("area_team_id"));
                mainAccount.setBusTeamId(0L);
            } else if (tableName.contains("bus_team")) {
                mainAccount.setMainAccountId(row.getLong("bus_team_id"));
                mainAccount.setName(row.getString("name"));
                mainAccount.setParentMainAccountId(row.getLong("area_team_id"));//区域
                mainAccount.setTag("busTeam");
                mainAccount.setAreaTeamId(row.getLong("area_team_id"));
                mainAccount.setBusTeamId(row.getLong("bus_team_id"));
            } else if (tableName.contains("teacher")) {
                mainAccount.setMainAccountId(row.getLong("teacher_id"));
                mainAccount.setName(row.getString("teacher_name"));
                mainAccount.setParentMainAccountId(row.getLong("bus_team_id") == null ? row.getLong("area_team_id") : row.getLong("bus_team_id"));
                mainAccount.setTag("teacher");
                mainAccount.setAreaTeamId(row.getLong("area_team_id"));
                mainAccount.setBusTeamId(row.getLong("bus_team_id") == null ? 0L : row.getLong("bus_team_id"));
            } else if (tableName.contains("student")) {
                mainAccount.setMainAccountId(row.getLong("student_id"));
                mainAccount.setName(row.getString("name"));
                mainAccount.setParentMainAccountId(1L);//无
                mainAccount.setTag("student");
                mainAccount.setAreaTeamId(0L);
                mainAccount.setBusTeamId(0L);
            }
            try {
                mainAccountService.insertSelective(mainAccount);
            } catch (Exception e) {
                mainAccountService.updateSelectiveById(mainAccount);
            }


        }

    }
}
